package breeze.pixel.weather.apps_util.views;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import breeze.pixel.weather.App;
import breeze.pixel.weather.apps_util.utils.appsettings.AppSettings;

public class AppLinearLayout extends LinearLayout {
    public AppLinearLayout(Context context) {
        super(context);
        init(context);
    }

    public AppLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AppLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private int transparency,radius;
    private void init(Context context){
        initParameters();
        IntentFilter intentFilter = new IntentFilter("breeze.pixel.weather.action.target.AppLinearLayout");
        context.registerReceiver(new AppLinearLayoutReceiver(),intentFilter);
    }

    private void initParameters() {
        AppSettings appSettings = App.getAppSettings();
        transparency = appSettings.getCardViewTransparency();
        radius = appSettings.getCardViewRadius();
        setRadiusAndTrans(appSettings.getCardViewRadius(),appSettings.getCardViewTransparency());
    }

    public int getTransparency() {
        return transparency;
    }

    public void setTransparency(int transparency) {
        this.transparency = transparency;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public void setRadiusAndTrans(int radius, int trans){
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setCornerRadius(radius);
        gradientDrawable.setColor(Color.argb(trans,255,255,255));
        setBackground(gradientDrawable);
    }

    class AppLinearLayoutReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            initParameters();
        }
    }
}
